<template>
  <div class="register" :style="{backgroundImage:'url('+image+')'}">
    <div class="image">
      <el-image :src="image" style="width: 100%"/>
    </div>
    <div class="form">
      <div class="box">
        <div style="margin-bottom: 40px">
          <span style="font-size: 35px;font-weight: 600;color: white">宿舍管理系统</span>
        </div>
        <el-form :model="form" class="register_form" :rules="rules" ref="login">
          <el-form-item></el-form-item>
          <el-form-item label="用户名:" prop="userName">
            <el-input v-model="form.userName"/>
          </el-form-item>
          <el-form-item label="密&#8195;码:" prop="password">
            <el-input v-model="form.password" type="password" autocomplete="off"/>
          </el-form-item>
          <el-form-item>
            <div style="display: flex;justify-content: space-between;width: 100%">
              <el-button type="primary" @click="pushLogin" style="width: 47%">注册</el-button>
              <el-button @click="pushClose" style="width: 47%">返回</el-button>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>

import axios from "axios";
import {ElMessage} from "element-plus";

export default {
  name: "register",
 
  data() {
  
    return {
      rules: {
        userName: [{
          pattern: /^[\u4e00-\u9fff]+$/,
          required: true,
          message: "只能注册中文字符,请重新输入",
          trigger: "blur"
        }],
        password:  [
          { required: true, message: '请输入数字', trigger: 'blur' },
          { pattern: /^\d+$/, message: '请输入正确的数字', trigger: 'blur' }
        ]
      },
      image: require("@/assets/login.png"),
      form: {
        userName: "",
        password: "",
        dormitoryId: undefined,
        name: undefined,
        phone: undefined,
        eMail: undefined,
        cardId: undefined
      }
    }
  },
  methods: {
    pushLogin() {
      this.$refs.login.validate((valid) => {
        if(valid){
          axios({
        url: "http://localhost:8888/user/add",
        method: "post",
        data: this.form
      }).then(res => {
        if (res.data) {
          ElMessage({
            message: "新增成功!",
            type: "success"
          })
          this.$router.push("/login")
        }
      })
        } else {
          ElMessage({
            message: "新增失败!",
            type: "danger"
          })
        }
      })
    },
    pushClose() {
      this.$router.push("/login")
    }
  }
}
</script>

<style scoped>

.image {
  width: 100%;
  height: 100%;
}

.box {
  padding: 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.form {
  width: 20%;
  position: absolute;
  top: 200px;
  right: 39.7%;
}

.register_form {
  width: 100%;
  padding: 1rem;
  background: rgba(255, 255, 255, 0.8);
}

::v-deep .el-image__inner {
  height: 749px !important;
}

::v-deep .el-form-item__label {
  width: 70px !important;
  font-weight: 600;
}

::v-deep .el-form-item__content {
  margin-left: 0px !important;
}

</style>